<template>
	<view class="main_class">
	<!-- 	<view class="class_h">
			<view class="top" @click="return_step">					
				<image src="/static/resource/back.png" mode="aspectFill" style="width: 74px;height: 30px;"></image>				
			</view>
		<!-- 	<text class="task_name">血糖历史数据</text> -->
		<!--</view> -->	
		
		<view class="top"></view>
		<view class='header'>
			<image :src="api_image+'/static/upload/535ccea5bd27c7e4/ecafe8593aa73c31.png'" mode="aspectFit"></image>
		</view>
		<!-- #ifdef MP-ALIPAY -->
		<button class='bottom' type='primary' withCredentials="true" lang="zh_CN" @click="alipayLogin">
			授权登录
		</button>
		<!-- #endif -->
		<!-- #ifdef MP-WEIXIN -->
		
		<button class='bottom' open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
			手机号快捷登录
		</button>
		<button class='bottom s' style="margin-top: -50rpx; background-color: #b9b2b2 !important;color: #333333 !important;" @click="cancel">
			取消/拒绝
		</button>
		<!-- #endif -->
		
		<view class="xy_bottom">
			<view style="width: 22rpx;height: 22rpx;">
			      <checkbox class="custom-checkbox" color="#10a1e0" ></checkbox>
			</view>
			<view class="flex ml-30" style="margin-top: 8rpx;">
				<span>
					我已阅读并同意
				</span>
				<span  class=" xy_info"  @click="agreement(1)">
					《平台隐私政策》和
				</span>
				<span  class=" xy_info"  @click="agreement(2)">《用户服务协议》</span>	
				
			</view>				
		</view>
			
	</view>
</template>


<script>
export default {
	data() {
		return {
			...getApp().globalData.config,
			recommendId:'',
			router:'',
			options:'',
			checkboxValue1:[],
			// 基本案列数据
			checkboxList1: [{
					name: '',
					disabled: false
				}
			],
		};
	},
	
	onLoad(options) {
		console.log('options',options)
	    if(options.recommendId) this.recommendId=options.recommendId
		if(options.scene){
			let scene = this.urlParams(options.scene)
			this.recommendId=scene.recommendId
			console.log('scene',scene)
		}
	 },
	created() {
		let pages = getCurrentPages() // 获取栈实例
		let prevPage = pages[pages.length - 2] // 获取上个页面的数据，包含页面
		this.router=prevPage.route
		this.options=prevPage.options
		// this.recommendId=prevPage.recommendId
	},
	methods: {	
		cancel(){
				this.$routers.router('/pages/home/index','switchTab') 
		},		
		// 将url中的参数转成对象{key:value}的形式
		urlParams (scene) {
			// scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
			const str = decodeURIComponent(scene).replace('?', '&')
			let strArr = str.split('&')
			strArr = strArr.filter(item => item)
			const result = {}
			strArr.filter(item => {
				const key = item.split('=')
				result[key[0]] = key[1]
			})
			return result
		},
		agreement(type){
				this.$routers.router('/pages/pagesuser/agreement/index?type='+type,'navigateTo') 
		},
		checkboxChange(n) {
		            console.log('change', n);
		},
		
		// 手机号登录
		getPhoneNumber (e) {
            let that = this
			let params = {
				code:e.detail.code,
			    recommendId:this.recommendId,
			} 			
			that.$http.login_phone(params).then(res=>{
				console.log('res----',res)
				if (res.resCode == '9999') {
					uni.showToast({
						title: '登陆成功',
						duration: 3000
					});	
					uni.setStorageSync('userInfo',res.data);
					console.log('ok')
					setTimeout(function(){
						that.checkFirst();	
					},3000)
				} else {
					uni.showToast({title:res.resMessage,icon:"none"})
				}								
			})		
		 },
		//微信登陆
		wxLogin(){
			let that = this
			let params = {
				authCode:'',
				nickName:'测试',
				avatarUrl:'',							 
			} 			
			that.$http.login(params).then(res=>{
				console.log('res----',res)
				if (res.resCode == '9999') {
					uni.showToast({
						title: '登陆成功',
						duration: 3000
					});	
					uni.setStorageSync('userInfo',res.data);
					console.log('ok')
					setTimeout(function(){
						that.checkFirst();	
					},3000)
				}								
			})		
		},
		// 调用支付宝小程序的授权登录
		alipayLogin() {
			my.getAuthCode({
				scopes: 'auth_user',
				success: (ress) => {
				  if (ress.authCode) {
					  my.getAuthUserInfo({
						   success: (res) => {
							  console.log("用户信息：" + JSON.stringify(res))
							  console.log('res',ress)
							  // 获取授权码成功，调用后端接口进行登录
							  this.handleThirdLoginApp(ress.authCode, res.nickName,res.avatar);
							},
							fail: (err) => {
							  // 处理获取授权码失败的情况
							  console.error('获取授权码失败：', err);
							}
							
					  });
					 
				  }
				},
				fail: (err) => {
				  // 处理获取授权码失败的情况
				  console.error('获取授权码失败：', err);
				}
			});
		},
		// wxGetUserInfo(){
		// 	let that=this
		// 	let obj_data={
		// 		token:'oPXGC6VsJb-vRn_soyUavutxhXTk',
		// 	    login_type:'app'
		// 	};
		// 	this.$http.getUser(obj_data).then(res=>{				
		// 		if(res.resCode=='9999'){
		// 			uni.showToast({
		// 				title: '登陆成功',
		// 				duration: 3000
		// 			});	
		// 			uni.setStorageSync('userInfo',res.data);
		// 			setTimeout(function(){
		// 				that.checkFirst();	
		// 			},3000)
					
		// 		}				
		// 	})
		// },
		
		//跳转路由
		checkFirst(){	
			let router=this.router
			console.log('router',router)
			switch(router){
				case 'pages/mine/index':				
					uni.switchTab({
						url: '/pages/mine/index'
					});				
					break;
				case 'pages/class/index':
					uni.switchTab({
						url: '/pages/class/index'
					});				
					break
				case 'pages/pagesGoods/detail/index':
					uni.redirectTo({
						url: '/pages/pagesGoods/detail/index?id='+this.options.id
					});				
					break
				case 'pages/front/goods/detail':
					uni.redirectTo({
						url: '/pages/front/goods/detail?goodsId='+this.options.goodsId
					});				
					break
				case 'pages/doll/index':
					uni.switchTab({
						url: '/pages/doll/index'
					});				
					break
				default:
				case 'pages/mine/index':
					uni.switchTab({
						url: '/pages/mine/index'
					});				
					break;
			}			
		},			
		//app第三方登录
		 handleThirdLoginApp(authCode,nickName,avatar) {
			 console.log('---ok',authCode)
			var that = this;								
			let params = {
				authCode,
				nickName,
				avatarUrl:avatar,	
				recommendId:this.recommendId
			} 			
			that.$http.login(params).then(res=>{
				console.log('res----',res)
				if (res.resCode == '9999') {
					uni.showToast({
						title: '登陆成功',
						duration: 3000
					});	
					uni.setStorageSync('userInfo',res.data);
					console.log('ok')
					setTimeout(function(){
						that.checkFirst();	
					},3000)
				}								
			})			
		}		
	}
};
</script>
<style>
	@import "./index.css";
</style>
